package com.example.liwushuo;

import java.util.ArrayList;
import java.util.List;
import com.example.liwushuo.fragment.HomeFragment;
import com.example.liwushuo.fragment.MyInfoFragment;
import com.example.liwushuo.fragment.OneProductFragment;
import com.example.liwushuo.fragment.SortFragment;
import com.example.liwushuo.utils.FragmentTabUtils;
import com.example.liwushuo.utils.GetUrl;
import com.example.liwushuo.utils.Utils;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Window;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {

	private RadioGroup radioGroup;
	private List<Fragment> list;
	private String gender;
	private String generation;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 去除ActionBar
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);

		gender = Utils.getParams(getApplicationContext(), "gender");
		generation = Utils.getParams(getApplicationContext(), "generation");

		radioGroup = (RadioGroup) findViewById(R.id.radioGroup_id);
		initFragment();

	}

	// 实现两次点击回车退出应用程序
	private long exitTime;

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {

		if (keyCode == KeyEvent.KEYCODE_BACK) {

			if ((System.currentTimeMillis() - exitTime) > 2000) {
				Toast.makeText(this, "再按一次退出程序", 1).show();
				exitTime = System.currentTimeMillis();
			} else {
				finish();
			}
		}

		return true;
	}

	private void initFragment() {
		Bundle args = null;

		HomeFragment home = new HomeFragment();
		args = new Bundle();
		args.putString("url", GetUrl.getTabUrl(gender, generation));
		home.setArguments(args);

		OneProductFragment one = new OneProductFragment();
		args = new Bundle();
		args.putString("url", GetUrl.getOneUrl(gender, generation));
		one.setArguments(args);
		list = new ArrayList<Fragment>();

		list.add(home);
		list.add(one);
		list.add(new SortFragment());
		list.add(new MyInfoFragment());

		new FragmentTabUtils(getSupportFragmentManager(), list,
				R.id.main_Fragment_id, radioGroup);

	}

}
